/*
 *
 * CallByJ
 * Copyright (C)2012-2012, Sandro Salari
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
package it.codestudio.callbyj;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.log4j.Logger;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

// TODO: Auto-generated Javadoc
/**
 * * The Class CallByJ.
 *
 * @author  Xandros ( sandro.salari AT gmail.com )
 * @author Last changed by: Xandros
 * @version 15-ago-2012
 * @since 1.0
 */
public class CallByJ {

	/** The Constant logger. */
	private static final Logger logger = Logger.getLogger(CallByJ.class);

	/** The options. */
	private static Options options = new Options();

	/** The parser. */
	private static CommandLineParser parser = new GnuParser();		

	/** The com manager. */
	private static ComManager comManager;
	
	private final static String QUEUE_NAME = "hello";

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main ( String[] args )
	{
		try{
			//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			//http://www.mkyong.com/java/java-properties-file-examples/
			Properties prop = new Properties();
			prop.load(new FileInputStream("config.properties"));
	 
	        //get the property value
	    	String audioCOM = prop.getProperty("audioCOM");
	    	String commandCOM =	prop.getProperty("commandCOM");
			Boolean playMessage = true;
			
			if(audioCOM != null && commandCOM !=null && !audioCOM.isEmpty() && !commandCOM.isEmpty()){
				comManager = ComManager.getInstance(commandCOM,audioCOM,playMessage);
			}else{
				logger.error("Can not load COM port number");
				return;
			}
			options = new Options();
			options.addOption("h", "help", false, "Print help for this application");
			options.addOption( "p", "pin", true, "Specify pin of device, if present" );
			options.addOption( "c", "call", true, "Start call to specified number" );
			options.addOption( "e", "end", false, "End all active call" );
			options.addOption( "t", "terminate", false, "Terminate application" );
			options.addOption( "r", "respond", false, "Respond to incoming call" );
			comManager.startModemCommandManager();		
			
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("localhost");
			Connection connection = factory.newConnection();
			Channel channel = connection.createChannel();

			channel.queueDeclare(QUEUE_NAME, false, false, false, null);
			System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
			    
			QueueingConsumer consumer = new QueueingConsumer(channel);
			channel.basicConsume(QUEUE_NAME, true, consumer);
			    
			while(true){
				try{
					QueueingConsumer.Delivery delivery = consumer.nextDelivery();
					String message = new String(delivery.getBody());
					System.out.println(" [x] Received '" + message + "'");

					comManager.sendCommandToModem(ATCommands.CALL,message);
					Thread.sleep(50000);
					comManager.sendCommandToModem(ATCommands.END_CALL);
					
				}catch (Exception e) {
					logger.error(e.getMessage(),e);
				}
			}
		}
		catch ( Exception e )
		{
			logger.error(e.getMessage(),e);
		}
		finally{
			if(comManager != null)
				comManager.terminate();
		}
	}

}
